<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="65px">
        <el-form-item label="物料编号">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="入库单号">
          <el-input
            v-model="searchParam.orderNo"
            placeholder="请输入入库单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!-- <el-form-item label="入库类型">
          <el-select
            v-model="searchParam.inboundTypeId"
            clearable
            placeholder="请选择入库类型"
          >
            <el-option
              v-for="item in inboundTypes"
              :key="item.id"
              :label="item.inboundTypeName"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="供应商">
          <el-input
            v-model="searchParam.partnerCode"
            placeholder="请输入供应商编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="存储批次">
          <el-input
            v-model="searchParam.stockBatch"
            placeholder="请输入存储批次"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="仓库">
          <el-select
            v-model="searchParam.warehouseId"
            clearable
            placeholder="请选择仓库"
            @change="warehouseChange"
          >
            <el-option
              v-for="item in warehouses"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="存储区">
          <el-select
            v-model="searchParam.storageSectionId"
            clearable
            placeholder="请选择存储区"
            @change="storageSectionChange"
          >
            <el-option
              v-for="item in storageSections"
              :key="item.id"
              :label="item.storageSectionName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="料架">
          <el-select
            v-model="searchParam.shelfId"
            clearable
            placeholder="请选择料架"
            @change="shelfChange"
          >
            <el-option
              v-for="item in shelfs"
              :key="item.id"
              :label="item.shelfName"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="仓位">
          <el-select
            v-model="searchParam.inId"
            clearable
            placeholder="请选择仓位"
          >
            <el-option
              v-for="item in bins"
              :key="item.id"
              :label="item.shelfName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="入库时间">
          <el-date-picker
            v-model="searchParam.storeTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getstoreTime"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column
          label="入库单号"
          prop="orderNo"
          min-width="170"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="入库类型名称"
          prop="inboundTypeName"
          min-width="120"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="入库时间"
          prop="storeTime"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="供应商编号"
          prop="partnerCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="供应商名称"
          prop="partnerName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="供应商批次"
          prop="supplyBatch"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column
          label="存储批次"
          prop="stockBatch"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="仓库名称"
          prop="warehouseName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="存储区名称"
          prop="storageSectionName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="料架名称"
          prop="shelfName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column
          label="仓位名称"
          prop="binName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="物料号"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="物料名称"
          prop="materialName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="总数量"
          prop="quantity"
          min-width="65"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="75"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="POSA"
          prop="poSa"
          min-width="90"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column
          label="条码号"
          prop="outCode"
          min-width="360"
          show-overflow-tooltip
          align="center"
        >
          <template #default="{ row }">
            {{ row.outCode }}
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="RFID"
          prop="rfId"
          min-width="240"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="300"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="更新人名"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <srmstore-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "wmsSRMSTORE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/wms/srmstore/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  orderNo: "",
  inboundTypeId: "",
  inboundTypeCode: "",
  inboundTypeName: "",
  storeTimeSearch: [],
  storeTimeStart: "",
  storeTimeEnd: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  partnerId: "",
  partnerCode: "",
  partnerName: "",
  supplyBatch: "",
  stockBatch: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTimeSearch: [],
  lastUpdateTimeStart: "",
  lastUpdateTimeEnd: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
};

const deleteParam = {
  api: "/wms/srmstore/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/wms/srmstore/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开入库单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改入库单";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增入库单";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开入库单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "入库单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getstoreTime(data: any) {
  if (data) {
    searchParam.value.storeTimeStart = data[0];
    searchParam.value.storeTimeEnd = data[1];
  } else {
    searchParam.value.storeTimeStart = "";
    searchParam.value.storeTimeEnd = "";
  }
}
function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}
function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/wms/srmstore/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
onMounted(() => {
  getTableList();
  getPartner();
  //getInboundType();
  getWarehouses();
});
let partners = ref([]);
async function getPartner() {
  const data = await request({
    url: "/wms/mdmpartner/getList",
    method: "get",
  });
  partners.value = data.data;
}
// let inboundTypes = ref([]);
// async function getInboundType() {
//   const data = await request({
//     url: "/wms/wmsinboundtype/getList",
//     method: "post",
//     data: {},
//   });
//   inboundTypes.value = data.data;
// }
let warehouses = ref([]);
async function getWarehouses() {
  const data = await request({
    url: "/wms/wmswarehouse/getList",
    method: "get",
  });
  warehouses.value = data.data;
}
function warehouseChange(id: string) {
  searchParam.value.storageSectionId = "";
  searchParam.value.shelfId = "";
  if (id) {
    //getStorageSections();
  }
}
// let storageSections = ref([]);
// async function getStorageSections() {
//   const data = await request({
//     url: "/wms/wmsstoragesection/getList/" + searchParam.value.warehouseId,
//     method: "get",
//   });
//   storageSections.value = data.data;
// }
// function storageSectionChange(id: string) {
//   searchParam.value.shelfId = "";
//   if (id) {
//     getShelf();
//   }
// }
// let shelfs = ref([]);
// async function getShelf() {
//   const data = await request({
//     url: "/wms/wmsshelf/getList/" + searchParam.value.storageSectionId,
//     method: "get",
//   });
//   shelfs.value = data.data;
// }
// function shelfChange(id: string) {
//   if (id) {
//     var shelf = shelfs.value.find((item) => {
//       return item.id === searchParam.value.shelfId;
//     });
//     searchParam.value.shelfCode = shelf.shelfCode;
//     searchParam.value.shelfName = shelf.shelfName;
//     getDins();
//   }
// }
// let storageTypes = ref([]);
// async function getStorageType() {
//   const data = await request({
//     url: "/wms/wmsstoragetype/getList",
//     method: "get",
//   });
//   storageTypes.value = data.data;
// }
let bins = ref([]);
async function getDins() {
  const data = await request({
    url: "/wms/wmsbin/getList/" + searchParam.value.shelfId,
    method: "get",
  });
  bins.value = data.data;
}
function binChange(id: string) {
  if (id) {
    var bin = bins.value.find((item) => {
      return item.id === searchParam.value.binId;
    });
    searchParam.value.binCode = bin.binCode;
    searchParam.value.binName = bin.binName;
  }
}
</script>
